package sample.java.controller.center;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import sample.java.controller.jiekou.TransData;


import java.io.File;
import java.util.ArrayList;

public class HomeCentreLocalController {
    private ArrayList<String> ltMusic = new ArrayList<>();
    @FXML
    private ListView ltLocalMusic;
    private TransData<String,String> transData;

    public HomeCentreLocalController() {

    }

    public void setTransData(TransData<String, String> transData) {
        this.transData = transData;
    }

    @FXML
    private void selectFile(){
        File ff = new File("D:\\KuGou\\");
        getLocalMusic(ff);
        ObservableList<String> observableList = FXCollections.observableList(ltMusic);
        ltLocalMusic.setItems(observableList);
        ltLocalMusic.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                String pp = ltMusic.get(newValue.intValue());
                if(transData != null){
                    transData.passData(pp,pp);
                }
            }
        });
    }

    public void getLocalMusic(File ff){
        File[] ltFile = ff.listFiles();
        if(ltFile != null){
            for (File fff : ltFile){
                if(fff.isFile()){
                    String path = fff.getPath().toString();
                    if(path.endsWith("mp3")){
                        System.out.println(path);
                        ltMusic.add(path);
                    }
                }else{
                    //getLocalMusic(fff);
                }
            }
        }
    }
}
